<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>
          <el-icon><Search /></el-icon>
          筛选搜索</span
        >
        <div>
          <el-button class="button" @click="resetFn">重置</el-button>
          <el-button class="button" type="primary" @click="searchParamsFn"
            >查询搜索</el-button
          >
        </div>
      </div>
    </template>
    <div class="search">
      <div class="input">
        <span>输入搜索:</span>
        <el-input v-model="form.id" placeholder="服务单号" />
      </div>
      <div class="input">
        <span>处理状态:</span>
        <div class="select">
          <el-select
            clearable
            v-model="form.status"
            class="m-2"
            placeholder="全部"
            size="large"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
      <div class="input">
        <span>申请时间:</span>
        <div class="block">
          <el-date-picker
            v-model="form.createTime"
            type="date"
            placeholder="请选择时间"
            value-format="YYYY-MM-DD"
          />
        </div>
      </div>
      <div class="input">
        <span>操作人员:</span>
        <el-input v-model="form.handleMan" placeholder="全部" />
      </div>
      <div class="input">
        <span>处理时间:</span>
        <div class="block">
          <el-date-picker
            v-model="form.handleTime"
            type="date"
            placeholder="请选择时间"
            value-format="YYYY-MM-DD"
          />
        </div>
      </div>
    </div>
  </el-card>
  <div class="dataList">
    <div class="dataList-one">
      <el-icon size="large"><Document /></el-icon>
      <span>数据列表</span>
    </div>
    <el-button type="primary" v-permission="'addUser'">添加</el-button>
  </div>
  <el-table
    :data="list"
    style="width: 100%"
    ref="multipleSelection"
    @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55" />
    <el-table-column fixed prop="id" label="服务单号" width="150" />
    <el-table-column prop="createTime" label="申请时间" width="180">
      <template #default="{ row }">
        <span>{{ dateFormat(row.createTime) }}</span></template
      >
    </el-table-column>
    <el-table-column prop="memberUsername" label="用户账号" width="120" />
    <el-table-column prop="productRealPrice" label="退款金额" width="120">
      <template #default="{ row }">
        <span>{{ moneyFormat(row.productRealPrice) }}</span></template
      >
    </el-table-column>
    <el-table-column prop="status" label="申请状态" width="120">
      <template #default="{ row }">
        <span>{{ row.status === 2 ? "已完成" : "待处理" }}</span></template
      >
    </el-table-column>
    <el-table-column prop="handleTime" label="处理时间" width="180">
      <template #default="{ row }">
        <span>{{ dateFormat(row.handleTime) }}</span></template
      >
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="180">
      <template #default="{ row }">
        <el-button size="small" @click="handleClick(row.id)"
          >查看详情</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <div class="batch">
    <el-select
      v-model="operation"
      class="m-2"
      placeholder="批量操作"
      size="large"
    >
      <el-option
        v-for="item in options4"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
    <el-button type="primary" @click="delFn">确定</el-button>
  </div>
  <ElConfigProvider :locale="zhCn">
    <div class="right">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        small
        @size-change="pageSizeChangeHandle"
        @current-change="pageChangeHandle"
      />
    </div>
  </ElConfigProvider>
</template>
<script setup lang="ts">
import { ElMessage, ElMessageBox } from "element-plus";
import { dateFormat, moneyFormat } from "@/utils";
import { Search, Document } from "@element-plus/icons-vue";
import { reactive } from "vue";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import useTable from "@/hooks/useTable";
import {
  deleteOrderSalesListApi,
  orderSalesApi,
} from "./api/api";
import { ref } from "vue";
import { useRouter } from "vue-router";
const multipleSelection = ref<OrderList[]>([]);
const operation = ref("0"); // 批量操作下拉选项
// 批量删除函数
const removeFn = () => {
  if (ids.value.length === 0) {
    ElMessage({
      type: "warning",
      message: "请选择需要操作的订单",
    });
    return;
  }
  ElMessageBox.confirm("您确定要批量删除掉这部分数据吗", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteOrderSalesListApi(ids.value).then((res) => {
        if (res.code === 200) {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          removeDataFn();
        }
      });
    })
    .catch(() => {});
};
// 批量删除函数判断
const delFn = () => {
  // 判断已选的操作方式 0 是批量删除数据
  if (operation.value == "0") {
    removeFn();
    // 0 批量删除
    console.log("批量删除");
  }
};
const ids = ref<number[]>([]);
//获取多选的数据
const handleSelectionChange = (SelectVal: OrderList[]) => {
  ids.value = [];
  SelectVal.forEach((item: any) => {
    ids.value.push(item.id);
  });
};

// 表格事件
const router = useRouter();
// 跳转查看订单页面函数
const handleClick = (id: number) => {
  router.push(`/oms/returnApplyDetail?id=${id}`);
};
// 重置搜索数据函数
const resetFn = ()=>{
  form.createTime='',
  form.id='',
  form.handleMan='',
  form.handleTime='',
  form.status=''
}
// 批量删除的数据
const options4 = [
  {
    value: "0",
    label: "批量删除",
  },
];

// 订单状态的数据
const options = [
  {
    value: "0",
    label: "待处理",
  },
  {
    value: "1",
    label: "退货中",
  },
  {
    value: "2",
    label: "已完成",
  },
  {
    value: "3",
    label: "已拒绝",
  },
];
// 响应式搜索数据
const form = reactive({
  // 服务单号
  id: "",
  // 申请时间
  createTime: "",
  // 处理时间
  handleTime: "",
  // 处理人员
  handleMan: "",
  // 收货人姓名/号码
  receiverKeyword: "",
  // 申请状态
  status: "",
});
// 封装的useTable导出
const {
  list,
  total,
  pageNum,
  pageSize,
  pageChangeHandle,
  pageSizeChangeHandle,
  searchParamsFn,
  removeDataFn,
} = useTable(orderSalesApi, form);
</script>
<style lang="less" scoped>
.right {
  display: flex;
  justify-content: flex-end;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
:deep(.el-card__header) {
  border: none;
  height: 30px;
}
.search {
  align-items: center;
  width: 800px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-left: 40px;
  .input {
    display: flex;
    justify-content: space-between;
    width: 350px;
    margin-bottom: 20px;
    align-items: center;
    .select {
      :deep(.el-input__inner) {
        height: 30px;
        width: 185px;
      }
    }
    .block {
      :deep(.el-input__wrapper) {
        height: 30px;
        width: 215px;
        margin-left: -18px;
      }
    }
  }
  //   height: 50px;
  span {
    width: 120px;
    margin-left: 30px;
    font-size: 15px;
  }
}
:deep(.cell) {
  text-align: center;
}
.dataList {
  box-shadow: 0 0 2px 1px #ccc;
  margin-bottom: 20px;
  padding: 10px;
  box-sizing: border-box;
  margin: 10px 0;
  display: flex;
  justify-content: space-between;
  height: 61px;
  align-items: center;
  .dataList-one {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
    }
  }
}
.batch {
  margin-top: 20px;
  :deep(.el-input__wrapper) {
    height: 30px;
    width: 160px;
    margin-right: 20px;
  }
}
</style>
